﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility; 

namespace Runda.CMS.Portal.Workspace
{
    public partial class ContractDocument : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }
        public Guid ContractID
        {
            get
            {
                if (Request.QueryString["ContractID"] != null)
                {
                    return new Guid(Request.QueryString["ContractID"]);
                }
                return Guid.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ContractID"] != null)
                {
                    BindGrid();
                }
            }
        }

        private void BindGrid()
        {
            if (ContractID == Guid.Empty) return;
            List<CtrtDocumentEntity> list = new CtrtDocumentBll().GetAll<CtrtDocumentEntity>(" and ContractID = '" + ContractID + "'");
            RepeaterDocuments.DataSource = list;
            RepeaterDocuments.DataBind();
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            UploadFileBaseInfo uploadInfo = this.fuFilePath.UploadFile(Server.MapPath("../Uploads"));
            if (uploadInfo.IsSuccess)
            {
                CtrtDocumentEntity entity = new CtrtDocumentEntity();
                entity.ContractID = ContractID;
                entity.DocumentName = txtDocumentName.Text;
                entity.FileName = uploadInfo.FileName;
                entity.FileOriginalName = uploadInfo.FileOriginalName;
                entity.FilePath = uploadInfo.FilePath;
                entity.CreatedBy = base.CurrentUserID;
                entity.CreatedOn = DateTime.Now;

                CtrtDocumentBll bll = new CtrtDocumentBll();
                bll.Insert(entity);
                BindGrid();
                ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('上传成功');", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('" + uploadInfo.Message + "')", true);
            }
        }

        protected void RepeaterDocuments_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "download")
            {
                WebUtility.DownloadFile(e.CommandArgument.ToString().TrimStart('\\'), "", "");
            }
        }

        protected void delete(object sender, EventArgs e)
        {
            if (documentids.Value.Trim() != "")
            {
                string[] documentidsArray = documentids.Value.Trim().Split(new char[] { ';' });
                for (int index = 0; index < documentidsArray.Length; index++)
                {
                    CtrtDocumentBll bll = new CtrtDocumentBll();
                    CtrtDocumentEntity documentEntity = bll.Get<CtrtDocumentEntity>(new Guid(documentidsArray[index]));
                    if (documentEntity != null)
                    {
                        bll.Delete(documentEntity);
                    }
                }
                BindGrid();
            }
        }
    }
}
